//
//  SearchBar.m
//  StaffTax
//
//  Created by 侯伟 on 2020/2/25.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "SearchBar.h"
#import "IFMMenu.h"
#import "RSSwapButton.h"

@interface SearchBar ()<UITextFieldDelegate>{
   
}

/// 筛选按钮
@property(nonatomic,strong)RSSwapButton* menuBtn;

/// 取消按钮
@property(nonatomic,strong)UIButton* cancelBtn;

/// 搜索框
@property(nonatomic,strong)UITextField* inputTextField;

@end

@implementation SearchBar

- (instancetype)init{
    if(self = [super initWithOrientation:(MyOrientation_Horz)]){
        self.padding = UIEdgeInsetsMake(0, 8, 0, 8);
        self.myHeight = 44.0;
        self.subviewHSpace = 8;
        
        _searchType = SearchType_Video;
        
        [self initViews];
    }
    return self;
}

- (void)initViews{
    
    MyLinearLayout* inputLayout = [[MyLinearLayout alloc] initWithOrientation:(MyOrientation_Horz)];
    inputLayout.backgroundColor = COLOR_BACKGEOUND;
    inputLayout.padding = UIEdgeInsetsMake(0, 10, 0, 10);
    inputLayout.myHeight = PX(32.0);
    inputLayout.myCenterY = 0;
    inputLayout.myHorzMargin = 0;
    inputLayout.weight = 1.0;
    inputLayout.layer.masksToBounds = YES;
    inputLayout.layer.cornerRadius = PX(32)/2;
    [self addSubview:inputLayout];
    
    RSSwapButton* menuBtn = [[RSSwapButton alloc] initWithFrame:CGRectMake(0, 0, 28, 44)];
    menuBtn.gap = 4;
    [menuBtn setTitle:@"视频" forState:UIControlStateNormal];
    [menuBtn setTitleColor:COLOR_BLACK_DARK forState:UIControlStateNormal];
    [menuBtn setImage:[UIImage imageNamed:@"common_menu_down"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(menuBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    menuBtn.titleLabel.font = FONT_FIT(12);
    menuBtn.mySize = CGSizeMake(44, 28);
    menuBtn.myCenterY = 0;
    menuBtn.myHorzMargin = 0;
    [menuBtn sizeToFit];
    [inputLayout addSubview:menuBtn];
    _menuBtn = menuBtn;
    
    //输入框
    UITextField* inputTextField = [[UITextField alloc] init];
    inputTextField.myHeight = 32;
    inputTextField.myHorzMargin = 0;
    inputTextField.weight = 1.0;
    inputTextField.myCenterY = 0;
    
    inputTextField.backgroundColor = COLOR_BACKGEOUND;
    inputTextField.borderStyle = UITextBorderStyleNone;
    inputTextField.font = FONT_FIT(14);
    inputTextField.textColor = COLOR_BLACK_DARK;
    inputTextField.tintColor = COLOR_THEME;
    inputTextField.delegate = self;
    inputTextField.leftViewMode = UITextFieldViewModeAlways;
    inputTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    inputTextField.returnKeyType = UIReturnKeySearch;
    [inputTextField addTarget:self action:@selector(textFieldValueDidChaned:) forControlEvents:UIControlEventEditingChanged];
    
    [inputLayout addSubview:inputTextField];
    _inputTextField = inputTextField;
    
    //搜索图片
    UIView* leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, PX(30), PX(30))];
    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_search_input"]];
    imageView.contentMode = UIViewContentModeCenter;
    imageView.frame = CGRectMake(0, 0, PX(30), PX(30));
    [leftView addSubview:imageView];
    inputTextField.leftView = leftView;
    
    //取消
    UIButton* cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:COLOR_WHITE forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = FONTMID(15);
    [cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    cancelBtn.myHorzMargin = 0;
    cancelBtn.myCenterY = 0;
    cancelBtn.myHeight = PX(21);
    cancelBtn.myWidth = PX(42);
    [cancelBtn sizeToFit];
    [self addSubview:cancelBtn];
    _cancelBtn = cancelBtn;
}

- (void)showMenu{
    
    [UIView animateWithDuration:0.15 animations:^{
        self.menuBtn.imageView.transform =  CGAffineTransformMakeRotation(-M_PI);
    }];
    
    IFMMenuItem* item0 = [IFMMenuItem itemWithImage:NULL title:@"视频" action:^(IFMMenuItem *item) {
        self->_searchType = item.index;
        [self.menuBtn setTitle:@"视频" forState:UIControlStateNormal];
        if(self.delegate && [self.delegate respondsToSelector:@selector(searchBar:typeDidChange:)]){
            [self.delegate searchBar:self typeDidChange:self->_searchType];
        }
    }];
    item0.index = SearchType_Video;
    
    IFMMenuItem* item1 = [IFMMenuItem itemWithImage:NULL title:@"演员" action:^(IFMMenuItem *item) {
        self->_searchType = item.index;
        [self.menuBtn setTitle:@"演员" forState:UIControlStateNormal];
        if(self.delegate && [self.delegate respondsToSelector:@selector(searchBar:typeDidChange:)]){
            [self.delegate searchBar:self typeDidChange:self->_searchType];
        }
    }];
    item1.index = SearchType_Performer;
    
    IFMMenu *menu = [[IFMMenu alloc] initWithItems:@[item0,item1]];
    menu.titleColor = COLOR_BLACK_DARK;
    menu.titleFont = FONT_FIT(12);
    menu.menuBackGroundColor = UIColor.whiteColor;
    menu.segmenteLineColor = COLOR_GRAY_LINE;
    menu.minMenuItemHeight = PX(32);
    menu.minMenuItemWidth = PX(60);
    menu.gapBetweenImageTitle = 0;
    menu.arrowHight = 0.f;
    menu.edgeInsets = UIEdgeInsetsMake(0, 20, 0, 20);
    menu.menuBackgroundStyle = IFMMenuBackgroundStyleLight;
    menu.containerViewBackGroundColor = UIColor.clearColor;
    menu.menuCornerRadiu = PX(10.0);
    menu.showShadow = YES;
    menu.selectedIndex = _searchType;
    menu.selectedColor = COLOR_THEME;
    menu.dismissBlock = ^{
        [UIView animateWithDuration:0.15 animations:^{
            self.menuBtn.imageView.transform = CGAffineTransformIdentity;
        }];
    };
    [menu showFromNavigationController:[UIViewController currentNavigationController] WithX:16 isFullScreen:YES];
}

#pragma mark -- setter getter

- (void)setSearchWord:(NSString *)searchWord{
    self.inputTextField.text = searchWord;
    [self.inputTextField sizeToFit];
}

- (NSString*)searchWord{
    return self.inputTextField.text;
}

- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    self.inputTextField.placeholder = placeholder;
    [self.inputTextField sizeToFit];
}

- (void)setSearchType:(SearchType)searchType{
    _searchType = searchType;
    if(searchType == SearchType_Video){
        [self.menuBtn setTitle:@"视频" forState:UIControlStateNormal];
    }else{
        [self.menuBtn setTitle:@"演员" forState:UIControlStateNormal];
    }
}

#pragma mark -- UITextFieldDelegate

//监听return 键的点击
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
    
}

- (void)textFieldValueDidChaned:(UITextField*)textField{
    
    if(textField.markedTextRange != NULL)
        return;
    if(self.delegate && [self.delegate respondsToSelector:@selector(searchBar: textDidChange:)]){
        
        NSString* text = textField.text;
        text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        [self.delegate searchBar:self textDidChange:text];
    }
}


#pragma mark -- action

- (void)cancelBtnClick:(UIButton*)sender{
    [self.inputTextField resignFirstResponder];
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(searchBarCancelButtonClicked:)]){
        [self.delegate searchBarCancelButtonClicked:self];
    }
}

- (void)menuBtnClick:(UIButton*)sender{
    [self.inputTextField resignFirstResponder];
    
    [self showMenu];
}

@end
